Ext.extend(app.module,{
    currentFormValues: {},
    init: function(){
        mainPanel = this;
        this.jtktpsDs = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'jtpsyj/listForJtktps.do'}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'},
                {name:'description'},
                {name:'projecttype'},
                {name:'state'},
                {name:'reporttime'},
                {name:'approvaltime'},
                {name:'planendtime'},
                {name:'endtime'},
                {name:'tasktime'},
                {name:'middletime'},
                {name:'finishtime'},
                {name:'filepath'},
                {name:'taskpath'},
                {name:'remain'},
                {name:'filename'},
                {name:'projectTypeName'},
                {name:'iscross'},
                {name:'approvedept'},
                {name:'rank'},
                {name:'shoolmoney'},
                {name:'remarks'},
                {name:'deptname'},
                {name:'names'},
                {name:'isleader'},
                {name:'isagree'},
                {name:'yjpath'}
            ])
        });
        var jtktps_pagesize_combo = new Ext.form.ComboBox( {       //设置分页栏的每页记录数选择
            name : 'pagesize',
            hiddenName : 'pagesize',
            typeAhead : true,
            triggerAction : 'all',
            lazyRender : true,
            mode : 'local',
            store : new Ext.data.ArrayStore(
                {
                    fields : [ 'value', 'text' ],
                    data : [ [ 20, '20条/页' ],[ 50, '50条/页' ], [ 100, '100条/页' ],[ 200, '250条/页' ]]
                }),
            valueField : 'value',
            displayField : 'text',
            value : '20',
            editable : false,
            width : 85
        });
        var number = parseInt(jtktps_pagesize_combo.getValue());

        jtktps_pagesize_combo.on("select", function(comboBox) {
            var bbar = Ext.getCmp('jtktpsbbar');
            bbar.pageSize = parseInt(comboBox.getValue());
            number = parseInt(comboBox.getValue());
            mainPanel.jtktpsDs.reload( {
                params : {
                    start : 0,
                    limit : bbar.pageSize
                }
            });
        });
        this.body = new Ext.grid.GridPanel({
            store: this.jtktpsDs,
            region: 'center',
            loadMask: true,
            viewConfig:{
                forceFit:true
            },
            columns: [
                new Ext.grid.RowNumberer(),
                {
                    xtype: 'numbercolumn',
                    dataIndex: 'id',
                    header: 'id',
                    hidden: true
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'name',
                    header: '点击课题名称查看',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        return '<a href="javascript:void(0)" onclick="mainPanel.getFileViewDlg('+
                            record.data["id"]+')">'+value+'</a>';
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'approvedept',
                    header: '来源部门'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'projectTypeName',
                    header: '课题类型'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'names',
                    header: '负责人',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        if(value.indexOf(",")==-1){
                            return value;
                        } else {
                            return value.substring(0,value.indexOf(","));
                        }
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'deptname',
                    header: '负责人部门'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'isleader',
                    header: '专家组组长',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        if(value==1) return '是';
                        else return '否';
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'isagree',
                    header: '您的意见',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        if(value==1) return '通过';
                        else if(value==0) return '否';
                        else return "还未提交意见";
                    }
                },
                {
                    xtype: 'actioncolumn',
                    width: 50,
                    header: '是否通过',
                    align: 'center',
                    items: [
                        {
                            icon: '../images/ext/accept.png',
                            tooltip: '通过',
                            handler: function(grid, rowIndex, colIndex) {
                                var record = grid.getStore().getAt(rowIndex);
                                var yjpath = record.data.yjpath;
                                if(record.data.isleader){
                                    mainPanel.getJtktpsDialog(record.data.id,1,yjpath);
                                } else {
                                    mainPanel.submitPsyj(record.data.id,1);
                                }
                            }
                        },
                        {
                            icon: '../images/ext/delete.png',
                            tooltip: '不通过',
                            handler: function(grid, rowIndex, colIndex) {
                                var record = grid.getStore().getAt(rowIndex);
                                var yjpath = record.data.yjpath;
                                if(record.data.isleader){
                                    mainPanel.getJtktpsDialog(record.data.id,0,yjpath);
                                } else {
                                    mainPanel.submitPsyj(record.data.id,0);
                                }
                            }
                        }
                    ]
                }
            ],
            bbar: {
                id: 'jtktpsbbar',
                xtype: 'paging',
                pageSize : number,
                store : this.jtktpsDs,
                displayInfo : true,
                displayMsg : '显示{0}条到{1}条,共{2}条',
                emptyMsg : "没有符合条件的记录",
                items : [ '-', '&nbsp;&nbsp;', jtktps_pagesize_combo ]
            }
        });
        this.main.add(this.body);
        this.main.doLayout();
        this.jtktpsDs.load({
            params: {start:0, limit:20 }
        });
    },
    getJtktpsDialog: function(projectid,isagree,yjpath){
        mainPanel = this;
        var form = new Ext.form.FormPanel({
            labelAlign: 'right',
            bodyStyle: 'padding:5px',
            buttonAlign: 'right',
            frame: true,
            url:'jtpsyj/uploadSmjtpsyj.do',
            labelWidth: 80,
            allowBlank: false,
            msgTarget: 'side',
            defaults: {
                anchor: '90%'
            },
            items: [
                {
                    xtype: 'textarea',
                    name: 'yjpath',
                    allowBlank:false,
                    value:yjpath,
                    fieldLabel: '评审意见',
                    height:120
                }
            ],
            buttons: [{
                text: '提交',
                scope: this,
                handler: function() {
                    if (form.getForm().isValid()) {
                        form.getForm().submit({
                            waitTitle: '保存数据',
                            waitMsg: '正在保存……',
                            scope: this,
                            method: 'POST',
                            params: {projectid:projectid},
                            submitEmptyText: false,     //禁止提交emptyText
                            success: function(form, action) {
                                if (action.result.success==true) {
                                    mainPanel.submitPsyj(projectid,isagree);
                                    dlg.close();
                                } else {
                                    Ext.Msg.alert('信息', action.result.info);
                                }
                            },
                            failure: function(form, action) {
                                if(action.result.sessionstatus=='timeout'){
                                    alert("出于安全原因，请重新登录！");
                                    //发现请求超时，退出处理代码...
                                    var win = window;
                                    while (win != win.parent){
                                        win = win.parent;
                                    }
                                    win.location = "index.jsp";
                                } else {
                                    Ext.Msg.alert('提示', action.result.info);
                                }
                            }
                        });
                    }
                }
            }]
        });
        var dlg = new Ext.Window({
            height: 200,
            width: 500,
            layout: 'fit',
            border: false,
            frame: true,
            title: '您是专家组长，请上传书面评审意见',
            modal: true,
            plain: true,
            resizable: false,
            items: [form],
            listeners: {
                scope: this,
                render: function(fp){
                    form.getForm().waitMsgTarget = fp.getEl();
                },
                show: function(){
                    var f = form.getForm();
                    f.clearInvalid();
                }
            }
        });
        dlg.show();
    },
    submitPsyj: function(projectid,isagree){
        mainPanel = this;
        Ext.Ajax.request({
            method:'post',
            url:'jtpsyj/submitPsyj.do',
            params:{
                isagree:isagree,
                projectid:projectid
            },
            success: function(resp, opts) {
                var result = Ext.util.JSON.decode(resp.responseText);
                var info = result.info;
                if(result.success=='true') {
                    mainPanel.jtktpsDs.reload();
                    Ext.Msg.alert('信息', info);
                }else {
                    Ext.Msg.alert('信息', info);
                }
            },
            failure: function(resp,opts) {
                var result = Ext.util.JSON.decode(resp.responseText);
                var info = result.info;
                Ext.Msg.alert('提示', info);
            }
        });
    }
});